package logic

import (
	"context"
	"day15/model/mysql"
	"day15/rpc/user"

	"day15/rpc/internal/svc"
	"day15/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserListLogic) UserList(in *pb.UserListReq) (*pb.UserListResp, error) {
	page := in.Page
	pageSize := in.PageSize
	userModel := mysql.User{}
	users, total, err := userModel.Paginate(int(page), int(pageSize))
	if err != nil {
		l.Error("分页查询失败：", err)
		return nil, err
	}
	var userInfoList []*pb.UserInfo
	for _, v := range users {
		goodsInfo := user.UserInfo{
			Id:       int64(v.ID),
			Username: v.Username,
			Phone:    v.Phone,
			Email:    v.Email,
			Avatar:   v.Avatar,
			IdCard:   v.IdCard,
		}
		userInfoList = append(userInfoList, &goodsInfo)
	}
	return &pb.UserListResp{
		Total:    total,
		UserList: userInfoList,
	}, nil
}
